#include "mainwindowtcp.h"
#include "ui_mainwindowtcp.h"

MainWindowTcp::MainWindowTcp(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindowTcp)
{
    ui->setupUi(this);

    tcpSocket = new QTcpSocket;

//    QString ip = ui->lineEdit_ip->text();
//    QString port = ui->lineEdit_port->text();
    QString ip = "192.168.2.46";
    QString port = "60000";

    //2、向服务器发起连接
    tcpSocket->connectToHost(QHostAddress(ip),port.toUInt());

    //当客户端与服务器连接成功的时候，tcpSocket对象 会自动发射信号
    connect(tcpSocket,&QTcpSocket::connected,this,&MainWindowTcp::onConnectedSlot);

    //当有数据接受的时候，tcpSocket 会自动触发 readyRead 信号,在对应的槽函数中获取数据
    connect(tcpSocket,&QTcpSocket::readyRead,this,&MainWindowTcp::onRecvDataSlot);
}

MainWindowTcp::~MainWindowTcp()
{
    delete ui;
}

void MainWindowTcp::onConnectedSlot()
{
    qDebug() << "connect success";
}

void MainWindowTcp::onRecvDataSlot()
{
    QByteArray data = tcpSocket->readAll();
    //显示到界面上
    //ui->textBrowser->append(QString::fromLocal8Bit(data));
}

//发送数据的按钮
//void MainWindowAccount::on_btn_register_clicked()
//{
//    QString data = ui->textEdit->toPlainText();
//    tcpSocket->write(data.toLocal8Bit().data());
//}
